#include <stdlib.h>
#include "Flame.h"
#include "Transform.h"
#include "variables.h"

Flame::Flame(void)
{
	period = 2;
	tx = ty = tz = 0;
}


Flame::~Flame(void)
{
}


void Flame::draw( glm::mat4 global_transform){

		
	      if( (animate%period) == 0){ //time to update transform
			  //printf("update! %d, %d\n", period, animate);
			  tx += ((float)(rand()%100)-50)/1000.0;
			  ty += ((float)(rand()%100)-50)/1000.0;
			  tz += ((float)(rand()%100)-50)/1000.0;
			  if( tx > .07)
				  tx = .07;
			  if( ty > .07)
				  ty = .07;
			  if( tz > .07)
				  tz = .07;
			  if( tx < -.07)
				  tx = -.07;
			  if( ty < 0)
				  ty = 0;
			  if( tz < -.07)
				  tz = -.07;
		  }

		  glLoadIdentity();
		  glm::mat4 transf = Transform::translate(tx,ty,tz) *(this->transform);
		  glm::mat4 matx = glm::transpose( (transf)*(global_transform));
		  glLoadMatrixf(&matx[0][0]);

		  glUniform4fv(ambientcol ,1, this->ambient);
		  glUniform4fv(diffusecol ,1, this->diffuse );
		  glUniform4fv(specularcol ,1, this->specular );
		  glUniform4fv( emissioncol ,1, this->emission);
		  glUniform1f(shininesscol, this->shininess);

			
		  glBegin(GL_TRIANGLES);
			glVertex3f(0,ty/5,0);
			glVertex3f(0,0,tz/5);
			glVertex3f(tx/5,0,0);
		  glEnd();
}